<?php
/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
/**
 * 管理员操作记录
 * @param $log_url 操作URL
 * @param $log_info 记录信息
 */
function adminLog($log_info) {
    $add['log_time'] = time();
    $add['admin_id'] = session('admin_id');
    $add['log_info'] = $log_info;
    $add['log_ip'] = getIP();
    $add['log_url'] = __ACTION__;
    M('admin_log')->add($add);
}
function getAdminInfo($admin_id) {
    return D('admin')->where("admin_id=$admin_id")->find();
}
function tpversion() {
    if (!empty($_SESSION['isset_push']))
        return false;
    $_SESSION['isset_push'] = 1;
    error_reporting(0); //关闭所有错误报告
    $app_path = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
    $version_txt_path = $app_path . '/Application/Admin/Conf/version.txt';
    $curent_version = file_get_contents($version_txt_path);
    $vaules = array(
        'domain' => $_SERVER['HTTP_HOST'],
        'last_domain' => $_SERVER['HTTP_HOST'],
        'key_num' => $curent_version,
        'install_time' => INSTALL_DATE,
        'cpu' => '0001',
        'mac' => '0002',
        'serial_number' => SERIALNUMBER,
    );
    $url = "http://service.xiarikui.com/index.php?m=Home&c=Index&a=user_push&" . http_build_query($vaules);
    stream_context_set_default(array('http' => array('timeout' => 3)));
    file_get_contents($url);
}
/**
 * 面包屑导航  用于后台管理
 * 根据当前的控制器名称 和 action 方法
 */
function navigate_admin() {
    $navigate = include APP_PATH . 'Common/Conf/navigate.php';
    $location = strtolower('Admin/' . CONTROLLER_NAME);
    $arr = array(
        $navigate[$location]['name'] => 'javascript:;',
        $navigate[$location]['action'][ACTION_NAME] => 'javascript:;',
    );
    return $arr;
}
function print_arr($arr){
	echo '<pre>';
	print_r($arr);
}
/**
 * 导出excel
 * @param $strTable	表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable, $filename) {
    header("Content-type: application/vnd.ms-excel");
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=" . $filename . "_" . date('Y-m-d') . ".xls");
    header('Expires:0');
    header('Pragma:public');
    echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . $strTable . '</html>';
}
/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '') {
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++)
        $size /= 1024;
    return round($size, 2) . $delimiter . $units[$i];
}
/**
 * 根据id获取地区名字
 * @param $regionId id
 */
function getRegionName($regionId) {
    $data = M('region')->where(array('id' => $regionId))->field('name')->find();
    return $data['name'];
}
function getMenuList($act_list) {
    //根据角色权限过滤菜单
    $menu_list = getAllMenu();
    if ($act_list != 'all') {
        $right = M('sys_menu')->where("id in ($act_list)")->getField('right', true);
        foreach ($right as $val) {
            $role_right .= $val . ',';
        }
        $role_right = explode(',', $role_right);
        foreach ($menu_list as $k => $mrr) {
            foreach ($mrr['sub_menu'] as $j => $v) {
                if (!in_array($v['control'] . 'Controller@' . $v['act'], $role_right)) {
                    unset($menu_list[$k]['sub_menu'][$j]); //过滤菜单
                }
            }
        }
    }
    return $menu_list;
}
/**
 * 获取所有菜单
 * @return array
 */
function getAllMenu() {
    return array(
        'system' => array('name' => '系统设置', 'icon' => 'fa-cog', 'sub_menu' => array(
                array('name' => '系统设置', 'act' => 'index', 'control' => 'System'),
			//	array('name' => '权限资源列表', 'act' => 'rightList', 'control' => 'System'),
            )),
        'access' => array('name' => '权限管理', 'icon' => 'fa-gears', 'sub_menu' => array(
                array('name' => '管理员列表', 'act' => 'index', 'control' => 'Admin'),
                array('name' => '角色管理', 'act' => 'role', 'control' => 'Admin'),
                array('name' => '管理员日志', 'act' => 'log', 'control' => 'Admin'),
            )),
		'goods' => array('name' => '商品管理', 'icon' => 'fa-book', 'sub_menu' => array(
                array('name' => '商品分类', 'act' => 'categoryList', 'control' => 'Goods'),
                array('name' => '商品列表', 'act' => 'goodsList', 'control' => 'Goods'),
                array('name' => '商品评论', 'act' => 'comment', 'control' => 'Goods'),
            )),
        'promotion' => array('name' => '促销管理', 'icon' => 'fa-book', 'sub_menu' => array(
            array('name' => '优惠券列表', 'act' => 'coupon', 'control' => 'Promotion'),
            array('name' => '满减列表', 'act' => 'full_down', 'control' => 'Promotion'),
           // array('name' => '特价列表', 'act' => 'special', 'control' => 'Promotion'),
            array('name' => '限购列表', 'act' => 'purchase', 'control' => 'Promotion'),
        )),
		'business' => array('name' => '商家管理', 'icon' => 'fa-user', 'sub_menu' => array(
                array('name' => '商家列表', 'act' => 'index', 'control' => 'Business'),
				array('name' => '提现列表', 'act' => 'withdraw', 'control' => 'Business'),
            )),
        'order' => array('name' => '订单管理', 'icon' => 'fa-money', 'sub_menu' => array(
            array('name' => '订单列表', 'act' => 'index', 'control' => 'Order'),
        )),
        'member' => array('name' => '会员管理', 'icon' => 'fa-user', 'sub_menu' => array(
                array('name' => '会员列表', 'act' => 'index', 'control' => 'User'),
            )),
        'Ad' => array('name' => '广告管理', 'icon' => 'fa-flag', 'sub_menu' => array(
                array('name' => '广告列表', 'act' => 'adList', 'control' => 'Ad'),
               // array('name' => '广告位置', 'act' => 'positionList', 'control' => 'Ad'),
            )),
		'feedback' => array('name' => '反馈管理', 'icon' => 'fa-comments', 'sub_menu' => array(
                array('name' => '反馈列表', 'act' => 'index', 'control' => 'Feedback'),
            )),
		'delivery' => array('name' => '配送管理', 'icon' => 'fa-comments', 'sub_menu' => array(
                array('name' => '配送列表', 'act' => 'index', 'control' => 'Delivery')
            )),
        'tools' => array('name' => '数据管理', 'icon' => 'fa-plug', 'sub_menu' => array(
            //array('name' => '插件列表', 'act' => 'index', 'control' => 'Plugin'),
                array('name' => '数据备份', 'act' => 'index', 'control' => 'Tools'),
                array('name' => '数据还原', 'act' => 'restore', 'control' => 'Tools'),
            )),
    );
}
function respose($res) {
    exit(json_encode($res));
}
